//
//  WoDeFenDianViewController.m
//  EJLX
//
//  Created by adc on 15/11/23.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "WoDeFenDianViewController.h"
#import "WoDeFenDianTableViewCell.h"
#import "SecondMyFenDian.h"
#import "HttpsManager.h"
#import "SecondMyFenDian.h"

@interface WoDeFenDianViewController ()

@end

@implementation WoDeFenDianViewController

-(void)viewWillAppear:(BOOL)animated
{
    [HttpsManager requestMyFenDian:@"1" completion:^(MyFenDian *fD)
     {
         
         _fenDian=fD;
         _listAry=_fenDian.listAry;
         NSLog(@"listAry.count   %lu",(unsigned long)_listAry.count);
         
         
         __weak WoDeFenDianViewController *weakSelf = self;
         [tabelView addPullToRefreshWithActionHandler:^{
             NSLog(@"当进行下拉刷新执行该方法");
             [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
         }];
         
         [tabelView addInfiniteScrollingWithActionHandler:^{
             [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
         }];

         [tabelView reloadData];
         
         
         
         
     }];

}
- (void)requestEnd{
    [tabelView.pullToRefreshView stopAnimating];
    
    [tabelView.infiniteScrollingView stopAnimating];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 20,WIDE, 44)];
    view.backgroundColor=RGB_COLOR(0x00c49f);
    
    [self.view addSubview:view];
    UIButton*backBut=[[UIButton alloc]initWithFrame:CGRectMake(10,7,30, 30)];
    [backBut setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal];
    [backBut addTarget:self action:@selector(backBut) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:backBut];
    UILabel*nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(WIDE/2-50, 10+20, 100, 24)];
    nameLabel.text=@"我的分店";
    nameLabel.textAlignment=NSTextAlignmentCenter;
    nameLabel.textColor=[UIColor whiteColor];
    [self.view addSubview:nameLabel];
//    UISearchBar*searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(50, 5, WIDE-100, 34)];
//    searchBar.placeholder=@"商户姓名／电话";
//    searchBar.delegate=self;
//    [view addSubview:searchBar];
//    UIButton*fenDianBut=[[UIButton alloc]initWithFrame:CGRectMake(WIDE-50, 10,40, 24)];
//    [fenDianBut setTitle:@"筛选" forState:UIControlStateNormal];
//    [fenDianBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [fenDianBut addTarget:self action:@selector(shaiXuanBut) forControlEvents:UIControlEventTouchUpInside];
//    [view addSubview:fenDianBut];
    for (int i=0;i<4;i++)
    {
        UIButton*but=[[UIButton alloc]initWithFrame:CGRectMake(0+WIDE/4*i, 64, WIDE/4, 40)];
        //NSArray*ary=[NSArray arrayWithObjects:@"全部", @"商户",@"升级",@"代理",nil];
        NSArray*ary=[[NSArray alloc]initWithObjects:@"待完善", @"待审核",@"通过",@"全部",nil];
        
        [but setTitle:ary[i] forState:UIControlStateNormal];
        but.font=[UIFont systemFontOfSize:12];
        [but setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
        [but setTitleColor:RGB_COLOR(0x00c49f) forState:UIControlStateSelected];
        
        but.tag=i+1;
        if (i==0)
        {
            but.selected=YES;
            _select=but.tag;
            _a=0;
        }

        but.backgroundColor=RGB(240, 240, 240);
        [but addTarget:self action:@selector(butClcik:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:but];
        
    }
    label=[[UILabel alloc]initWithFrame:CGRectMake(0, 64+40,WIDE/4,1)];
    label.backgroundColor=RGB_COLOR(0x00c49f);
    [self.view addSubview:label];
    tabelView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64+40+5+10,WIDE , HEIGHT-64-40-5-50-50-10) style:UITableViewStylePlain];
    tabelView.delegate=self;
    tabelView.dataSource=self;
    tabelView.rowHeight=120;
    [self.view addSubview:tabelView];
    [tabelView registerNib:[UINib nibWithNibName:@"WoDeFenDianTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    UIView*view1=[[UIView alloc]initWithFrame:CGRectMake(0,HEIGHT-56, WIDE,56)];
    view1.backgroundColor=RGB(240, 240, 240);
    [self.view addSubview:view1];
    //UISegmentedControl*seg=[[UISegmentedControl alloc]initWithItems:@[@""]
    //UIView*smellView=[UIView alloc]initWithFrame:CGRectMake(0,HEIGHT, <#CGFloat width#>, <#CGFloat height#>)
    for (int i=0;i<3;i++)
    {
        NSArray*ary=[NSArray arrayWithObjects:@"间接分店",@"直接分店",@"全部分店", nil];
        UILabel*label2=[[UILabel alloc]initWithFrame:CGRectMake(WIDE/3*i,30, WIDE/3,26)];
        //label.tag=i;
        label2.textAlignment=NSTextAlignmentCenter;
        label2.font=[UIFont boldSystemFontOfSize:12];
        label2.text=ary[i];
        label2.textColor=[UIColor blackColor];
        UILabel*label1=[[UILabel alloc]initWithFrame:CGRectMake(WIDE/3*i,0,WIDE/3,30)];
        label1.textColor=RGB_COLOR(0x00c49f);
        label1.tag=i;
        NSString*str1=[NSString stringWithFormat:@"%d",_fenDian.totalAgent];
        NSString*str2=[NSString stringWithFormat:@"%d",_fenDian.totalVender];
        int a=_fenDian.totalAgent+_fenDian.totalVender;
        NSString*str3=[NSString stringWithFormat:@"%d",a];
        NSArray*ary1=[NSArray arrayWithObjects:str1,str2,str3, nil];
        label1.text=ary1[i];
        label1.font=[UIFont boldSystemFontOfSize:12];
        label1.textAlignment=NSTextAlignmentCenter;
        //label1.backgroundColor=[UIColor greenColor];
        [view1 addSubview:label1];
        //label2.backgroundColor=[UIColor greenColor];
        
        [view1 addSubview:label2];
        if (i==2)
        {
            label1.textColor=[UIColor whiteColor];
            label2.textColor=[UIColor whiteColor];
            label1.backgroundColor=RGB_COLOR(0x00c49f);
            label2.backgroundColor=RGB_COLOR(0x00c49f);
            
        }
        // NSArray*ary=[NSArray arrayWithObjects:@"", nil]
       
        
        
    }
    // Do any additional setup after loading the view, typically from a nib.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listAry.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondMyFenDian*secondMy=_listAry[indexPath.row];
    
    WoDeFenDianTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell.tuPian sd_setImageWithURL:[NSURL URLWithString:secondMy.avatar]];
    cell.shangJia.text=secondMy.level;
    cell.name.text=secondMy.owerName;
    cell.phone.text=secondMy.phone;
    cell.fenXiaoShang.text=[NSString stringWithFormat:@"%d",secondMy.agentCount];
    cell.jingXiaoShang.text=[NSString stringWithFormat:@"%d",secondMy.vender];
    cell.fenXiaoShang1.text=[NSString stringWithFormat:@"%d",secondMy.agentCount1];
    cell.jingXiaoShang1.text=[NSString stringWithFormat:@"%d",secondMy.vender1];
    cell.butClick.tag=indexPath.row;
    [cell.butClick addTarget:self action:@selector(phoneBut:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    
    return cell;
}
#pragma 电话
-(void)phoneBut:(UIButton*)sender
{
    SecondMyFenDian*phone=_listAry[sender.tag];
    NSString*num=[[NSString alloc]initWithFormat:@"telprompt://%@",phone.phone];
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:num]];
    
    
    
    }
-(void)butClcik:(UIButton*)sender
{
    
    UIButton*lastBut=(UIButton*)[self.view viewWithTag:_select];
    lastBut.selected=NO;
    
    sender.selected=YES;
    _select=sender.tag;
    _a=sender.tag-1;
    [UIView animateWithDuration:1 animations:^{
        label.frame=CGRectMake(WIDE/4*(sender.tag-1), 64+40, WIDE/4,1);
    } completion:^(BOOL finished)
     {
         
     }];
    NSString*srt=[NSString stringWithFormat:@"%ld",(long)sender.tag];
    [HttpsManager requestMyFenDian:srt completion:^(MyFenDian *fD)
    {
       
        _fenDian=fD;
        _listAry=_fenDian.listAry;
        _a=_fenDian.totalAgent;
        _b=_fenDian.totalVender;
        _c=_a+_b;
        NSLog(@"listAry.count   %lu",(unsigned long)_listAry.count);
        [tabelView reloadData];
        
        
        
        
    }];
    

    
    
    
    
}
-(CGFloat)getHeightByString:(NSString*)string   withFont:(UIFont*)font
{
    CGSize size = [string boundingRectWithSize:CGSizeMake(300, LONG_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
    return size .width;
}
//返回按钮
-(void)backBut
{
    [self.navigationController popViewControllerAnimated:YES];
}
//分店
-(void)shaiXuanBut
{
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
